package com.crayon.util;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.crayon.account.data.ItemGroup;
import com.crayon.account.fragments.ui.FragmentPieChart;
import com.crayon.account.fragments.ui.FragmentPieChart.OnPieChartDataBinder;
import com.crayon.sharemenu.R;

import org.afree.chart.AFreeChart;
import org.afree.chart.ChartFactory;
import org.afree.chart.plot.PiePlot;
import org.afree.chart.title.TextTitle;
import org.afree.data.general.DefaultPieDataset;
import org.afree.data.general.PieDataset;
import org.afree.graphics.geom.Font;

import java.util.List;

/**
 * com.crayon.util.UTILPieChartDemo01View
 * @author kws
 *
 */
public class UTILPieChartDemo01View extends APieChartView {

	
	
	OnPieChartDataBinder onPieChartDataBinder;
    /**
     * constructor
     * @param context
     */
    public UTILPieChartDemo01View(Context context,OnPieChartDataBinder onPieChartDataBinder) {
        super(context);
        this.onPieChartDataBinder = onPieChartDataBinder;

        PieDataset dataset = this.onPieChartDataBinder.OnPieDataSet();
        AFreeChart chart = this.onPieChartDataBinder.createChart(dataset);
//        final PieDataset dataset = createDataset();
//        final AFreeChart chart = createChart(dataset);

        setChart(chart);
    }
    
    public UTILPieChartDemo01View(Context context,AFreeChart chart) {
        super(context);
        setChart(chart);
    }


    /**
     * Creates a sample dataset.
     * @return a sample dataset.
     */
    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;
    }

    /**
     * Creates a chart.
     * @param dataset the dataset.
     * @return a chart.
     */
    private static AFreeChart createChart(PieDataset dataset) {

        AFreeChart chart = ChartFactory.createPieChart(
        		"Pie Chart Demo 01", // chart title
                dataset, // data
                true, // include legend
                true,
                false);
        TextTitle title = chart.getTitle();
        title.setToolTipText("A title tooltip!");

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Typeface.NORMAL, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;

    }
    
    public static void OnCrateChartByArray(List<ItemGroup> items,String chartTitle,Fragment fragment,int fragId){
		DefaultPieDataset dataset = new DefaultPieDataset();
		for (ItemGroup itemGroup : items) {
		   String title = String.format("%s %s",
				   itemGroup.getKey()!=null ? itemGroup.getKey():fragment.getString(R.string.dao_key_nocate),
				   UTILCurrency.formatPrice(itemGroup.getPriceTotal()));
		   dataset.setValue(title, new Double(itemGroup.getTotalpercent()));
		}
		
        AFreeChart chart = ChartFactory.createPieChart(
        		chartTitle, // chart title
                dataset, // data
                true, // include legend
                true,
                false);
        TextTitle title = chart.getTitle();
        title.setToolTipText("A title tooltip!");

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("SansSerif", Typeface.NORMAL, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        
		FragmentTransaction tr = fragment.getChildFragmentManager().beginTransaction();
		FragmentPieChart fragmentPieChart =FragmentPieChart.onCreate(chart);
//		fragmentPieChart.setOnPieChartDataBinder(this);
		tr.replace(fragId, fragmentPieChart);
		tr.commit();
	}
//    public static onCreateChartByArrayFragment(FragmentActivity activity,ArrayList<DAOAccount>)
}
